// src/utils/js/base64js-wrapper.js

const base64js = (function () {
    // 模块加载器逻辑
    const moduleCache = {};

    function require(name) {
        if (moduleCache[name]) {
            return moduleCache[name].exports;
        }

        const module = { exports: {} };
        moduleCache[name] = module;

        switch (name) {
            case "/":
                // base64 编码/解码模块实现
                const exports = {};
                const Uint8Array = typeof window !== 'undefined' && window.Uint8Array ? window.Uint8Array : Array;

                const lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

                const revLookup = [];
                for (let i = 0; i < lookup.length; ++i) {
                    revLookup[lookup.charCodeAt(i)] = i;
                }

                revLookup["-".charCodeAt(0)] = 62;
                revLookup["_".charCodeAt(0)] = 63;

                function getLens(b64) {
                    const len = b64.length;
                    if (len % 4 > 0) {
                        throw new Error("Invalid string. Length must be a multiple of 4");
                    }
                    const placeHolders = b64[len - 2] === "=" ? 2 : b64[len - 1] === "=" ? 1 : 0;
                    return [len, placeHolders];
                }

                function byteLength(b64) {
                    const lens = getLens(b64);
                    const length = lens[0];
                    const placeHolders = lens[1];
                    return ((length * 3) >> 2) - placeHolders;
                }

                function toByteArray(b64) {
                    const lens = getLens(b64);
                    const length = lens[0];
                    const placeHolders = lens[1];
                    const arr = new Uint8Array(((length / 4) * 3) - placeHolders);

                    let j = 0;
                    for (let i = 0; i < length; i += 4) {
                        const tmp =
                            revLookup[b64.charCodeAt(i)] << 18 |
                            revLookup[b64.charCodeAt(i + 1)] << 12 |
                            revLookup[b64.charCodeAt(i + 2)] << 6 |
                            revLookup[b64.charCodeAt(i + 3)];

                        arr[j++] = (tmp >> 16) & 0xff;
                        arr[j++] = (tmp >> 8) & 0xff;
                        arr[j++] = tmp & 0xff;
                    }

                    if (placeHolders === 2) {
                        const tmp =
                            revLookup[b64.charCodeAt(length - 2)] << 2 |
                            revLookup[b64.charCodeAt(length - 1)] >> 4;
                        arr[j++] = tmp & 0xff;
                    } else if (placeHolders === 1) {
                        const tmp =
                            revLookup[b64.charCodeAt(length - 2)] << 10 |
                            revLookup[b64.charCodeAt(length - 1)] << 4 |
                            revLookup[b64.charCodeAt(length - 3)] >> 2;
                        arr[j++] = (tmp >> 8) & 0xff;
                        arr[j++] = tmp & 0xff;
                    }

                    return arr;
                }

                function tripletToBase64(num) {
                    return (
                        lookup[(num >> 18) & 0x3f] +
                        lookup[(num >> 12) & 0x3f] +
                        lookup[(num >> 6) & 0x3f] +
                        lookup[num & 0x3f]
                    );
                }

                function encodeChunk(uint8, start, end) {
                    const tmp = [];
                    for (let i = start; i < end; i += 3) {
                        const a = uint8[i];
                        const b = uint8[i + 1];
                        const c = uint8[i + 2];
                        tmp.push(
                            tripletToBase64((a << 16) + (b << 8) + c)
                        );
                    }
                    return tmp.join("");
                }

                function fromByteArray(uint8) {
                    let tmp;
                    const len = uint8.length;
                    const extraBytes = len % 3;
                    const lenMain = len - extraBytes;
                    const result = [];

                    for (let i = 0; i < lenMain; i += 16383) {
                        result.push(encodeChunk(uint8, i, i + 16383 < lenMain ? i + 16383 : lenMain));
                    }

                    if (extraBytes === 1) {
                        const temp = uint8[len - 1];
                        result.push(lookup[temp >> 2] + lookup[(temp << 4) & 0x3f] + "==");
                    } else if (extraBytes === 2) {
                        const temp = (uint8[len - 2] << 8) + uint8[len - 1];
                        result.push(
                            lookup[temp >> 10] +
                            lookup[(temp >> 4) & 0x3f] +
                            lookup[(temp << 2) & 0x3f] +
                            "="
                        );
                    }

                    return result.join("");
                }

                exports.byteLength = byteLength;
                exports.toByteArray = toByteArray;
                exports.fromByteArray = fromByteArray;

                return exports;

            default:
                throw new Error("Module not found: " + name);
        }
    }

    function defineEntryPoint() {
        const entry = require("/");
        return entry;
    }

    // 兼容不同环境
    if (typeof exports === "object" && typeof module !== "undefined") {
        module.exports = defineEntryPoint();
    } else if (typeof define === "function" && define.amd) {
        define([], defineEntryPoint);
    } else {
        var g;
        if (typeof window !== "undefined") {
            g = window;
        } else if (typeof global !== "undefined") {
            g = global;
        } else if (typeof self !== "undefined") {
            g = self;
        } else {
            g = this;
        }
        g.base64js = defineEntryPoint();
    }

    // 返回最终的 base64js 对象（供 export default 使用）
    return defineEntryPoint();
})();

export default base64js;